<?php
/**
* Numedia Studio Framework
* 
* Admin functions library.
*
* This is called by the theme "init_admin.php"
*/


if (!function_exists( 'ns_options_parser')):
/**
 * Output options as html menues
 *
 * @since Twenty Eleven 1.0
 */
function ns_options_parser($options) {

    $counter = 0;
	$menu = '';
	$output = '';
	foreach ($options as $value) {

		$counter++;
		$val = '';
		//Start Heading
		 if ( $value['type'] != "heading" )
		 {
		 	$class = ''; if(isset( $value['class'] )) { $class = $value['class']; }
			//$output .= '<div class="section section-'. $value['type'] .'">'."\n".'<div class="option-inner">'."\n";
			$output .= '<div class="section section-'.$value['type'].' '. $class .'">'."\n";
			$output .= '<h3 class="heading">'. $value['name'] .'</h3>'."\n";
			$output .= '<div class="option">'."\n" . '<div class="controls">'."\n";

		 }
		 //End Heading
		$select_value = '';
		switch ( $value['type'] ) {

		case 'text':
			$val = $value['std'];
			$std = get_option($value['id']);
			if ( $std != "") { $val = $std; }
			$val = stripslashes( $val ); // Strip out unwanted slashes.
			$output .= '<input class="woo-input" name="'. $value['id'] .'" id="'. $value['id'] .'" type="'. $value['type'] .'" value="'. esc_attr( $val ) .'" />';
		break;

		case 'select':

			$output .= '<div class="select_wrapper"><select class="woo-input" name="'. $value['id'] .'" id="'. $value['id'] .'">';

			$select_value = stripslashes(get_option($value['id']));

			foreach ($value['options'] as $option) {

				$selected = '';

				 if($select_value != '') {
					 if ( $select_value == $option) { $selected = ' selected="selected"';}
			     } else {
					 if ( isset($value['std']) )
						 if ($value['std'] == $option) { $selected = ' selected="selected"'; }
				 }

				 $output .= '<option'. $selected .'>';
				 $output .= $option;
				 $output .= '</option>';

			 }
			 $output .= '</select></div>';


		break;
		case 'select2':

			$output .= '<div class="select_wrapper"><select class="woo-input" name="'. $value['id'] .'" id="'. $value['id'] .'">';

			$select_value = stripslashes(get_option($value['id']));

			foreach ($value['options'] as $option => $name) {

				$selected = '';

				 if($select_value != '') {
					 if ( $select_value == $option) { $selected = ' selected="selected"';}
			     } else {
					 if ( isset($value['std']) )
						 if ($value['std'] == $option) { $selected = ' selected="selected"'; }
				 }

				 $output .= '<option'. $selected .' value="'.esc_attr($option).'">';
				 $output .= $name;
				 $output .= '</option>';

			 }
			 $output .= '</select></div>';


		break;
		case 'calendar':

			$val = $value['std'];
			$std = get_option($value['id']);
			if ( $std != "") { $val = $std; }
            $output .= '<input class="woo-input-calendar" type="text" name="'.$value['id'].'" id="'.$value['id'].'" value="'.esc_attr($val).'">';

		break;
		case 'time':
			$val = $value['std'];
			$std = get_option($value['id']);
			if ( $std != "") { $val = $std; }
			$output .= '<input class="woo-input-time" name="'. $value['id'] .'" id="'. $value['id'] .'" type="text" value="'. esc_attr($val) .'" />';
		break;
		case 'textarea':

			$cols = '8';
			$ta_value = '';

			if(isset($value['std'])) {

				$ta_value = $value['std'];

				if(isset($value['options'])){
					$ta_options = $value['options'];
					if(isset($ta_options['cols'])){
					$cols = $ta_options['cols'];
					} else { $cols = '8'; }
				}

			}
				$std = get_option($value['id']);
				if( $std != "") { $ta_value = stripslashes( $std ); }
				$output .= '<textarea ' . ( !current_user_can( 'unfiltered_html' ) && in_array( $value['id'], ns_disabled_if_not_unfiltered_html_option_keys() ) ? 'disabled="disabled" ' : '' ) . 'class="woo-input" name="'. $value['id'] .'" id="'. $value['id'] .'" cols="'. $cols .'" rows="8">'.esc_textarea( $ta_value ).'</textarea>';


		break;
		case "radio":

			 $select_value = get_option( $value['id']);

			 foreach ($value['options'] as $key => $option)
			 {

				 $checked = '';
				   if($select_value != '') {
						if ( $select_value == $key) { $checked = ' checked'; }
				   } else {
					if ($value['std'] == $key) { $checked = ' checked'; }
				   }
				$output .= '<input class="woo-input woo-radio" type="radio" name="'. $value['id'] .'" value="'. esc_attr( $key ) .'" '. $checked .' />' . $option .'<br />';

			}

		break;
		case "checkbox":

		   $std = $value['std'];

		   $saved_std = get_option($value['id']);

		   $checked = '';

			if(!empty($saved_std)) {
				if($saved_std == 'true') {
				$checked = 'checked="checked"';
				}
				else{
				   $checked = '';
				}
			}
			elseif( $std == 'true') {
			   $checked = 'checked="checked"';
			}
			else {
				$checked = '';
			}
			$output .= '<input type="checkbox" class="checkbox woo-input" name="'.  $value['id'] .'" id="'. $value['id'] .'" value="true" '. $checked .' />';

		break;
		case "multicheck":

			$std =  $value['std'];

			foreach ($value['options'] as $key => $option) {

			$ns_key = $value['id'] . '_' . $key;
			$saved_std = get_option($ns_key);

			if(!empty($saved_std))
			{
				  if($saved_std == 'true'){
					 $checked = 'checked="checked"';
				  }
				  else{
					  $checked = '';
				  }
			}
			elseif( $std == $key) {
			   $checked = 'checked="checked"';
			}
			else {
				$checked = '';                                                                                    }
			$output .= '<input type="checkbox" class="checkbox woo-input" name="'. $ns_key .'" id="'. $ns_key .'" value="true" '. $checked .' /><label for="'. $ns_key .'">'. $option .'</label><br />';

			}
		break;
		case "multicheck2":

			$std =  explode( ',',$value['std']);

			foreach ($value['options'] as $key => $option) {

			$ns_key = $value['id'] . '_' . $key;
			$saved_std = get_option($ns_key);

			if(!empty($saved_std))
			{
				  if($saved_std == 'true'){
					 $checked = 'checked="checked"';
				  }
				  else{
					  $checked = '';
				  }
			}
			elseif( in_array($key,$std)) {
			   $checked = 'checked="checked"';
			}
			else {
				$checked = '';                                                                                    }
			$output .= '<input type="checkbox" class="checkbox woo-input" name="'. $ns_key .'" id="'. $ns_key .'" value="true" '. $checked .' /><label for="'. $ns_key .'">'. $option .'</label><br />';

			}
		break;
		case "upload":

			if ( function_exists( 'ns_medialibrary_uploader' ) ) {

				$output .= ns_medialibrary_uploader( $value['id'], $value['std'], null ); // New AJAX Uploader using Media Library

			} else {

				$output .= ns_uploader_function($value['id'],$value['std'],null); // Original AJAX Uploader

			} // End IF Statement

		break;
		case "upload_min":

			if ( function_exists( 'ns_medialibrary_uploader' ) ) {

				$output .= ns_medialibrary_uploader( $value['id'], $value['std'], 'min' ); // New AJAX Uploader using Media Library

			} else {

				$output .= ns_uploader_function($value['id'],$value['std'],'min' ); // Original AJAX Uploader

			} // End IF Statement

			// $output .= ns_uploader_function($value['id'],$value['std'],'min' );

		break;
		case "color":
			$val = $value['std'];
			$stored  = get_option( $value['id'] );
			if ( $stored != "") { $val = $stored; }
			$output .= '<div id="' . $value['id'] . '_picker" class="colorSelector"><div></div></div>';
			$output .= '<input class="woo-color" name="'. $value['id'] .'" id="'. $value['id'] .'" type="text" value="'. esc_attr( $val ) .'" />';
		break;

		case "border":

			$default = $value['std'];
			$border_stored = get_option( $value['id'] );

			/* Border Width */
			$val = $default['width'];
			if ( $border_stored['width'] != "") { $val = $border_stored['width']; }
			$output .= '<select class="woo-border woo-border-width" name="'. $value['id'].'_width" id="'. $value['id'].'_width">';
				for ($i = 0; $i < 21; $i++){
					if($val == $i){ $active = 'selected="selected"'; } else { $active = ''; }
					$output .= '<option value="'. $i .'" ' . $active . '>'. $i .'px</option>'; }
			$output .= '</select>';

			/* Border Style */
			$val = $default['style'];
			if ( $border_stored['style'] != "") { $val = $border_stored['style']; }
				$solid = ''; $dashed = ''; $dotted = '';
			if($val == 'solid'){ $solid = 'selected="selected"'; }
			if($val == 'dashed'){ $dashed = 'selected="selected"'; }
			if($val == 'dotted'){ $dotted = 'selected="selected"'; }

			$output .= '<select class="woo-border woo-border-style" name="'. $value['id'].'_style" id="'. $value['id'].'_style">';
			$output .= '<option value="solid" '. $solid .'>Solid</option>';
			$output .= '<option value="dashed" '. $dashed .'>Dashed</option>';
			$output .= '<option value="dotted" '. $dotted .'>Dotted</option>';
			$output .= '</select>';

			/* Border Color */
			$val = $default['color'];
			if ( $border_stored['color'] != "") { $val = $border_stored['color']; }
			$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div></div></div>';
			$output .= '<input class="woo-color woo-border woo-border-color" name="'. $value['id'] .'_color" id="'. $value['id'] .'_color" type="text" value="'. esc_attr( $val ) .'" />';

		break;

		case "images":
			$i = 0;
			$select_value = get_option( $value['id']);

			foreach ($value['options'] as $key => $option)
			 {
			 $i++;

				 $checked = '';
				 $selected = '';
				   if($select_value != '') {
						if ( $select_value == $key) { $checked = ' checked'; $selected = 'woo-radio-img-selected'; }
				    } else {
						if ($value['std'] == $key) { $checked = ' checked'; $selected = 'woo-radio-img-selected'; }
						elseif ($i == 1  && !isset($select_value)) { $checked = ' checked'; $selected = 'woo-radio-img-selected'; }
						elseif ($i == 1  && $value['std'] == '') { $checked = ' checked'; $selected = 'woo-radio-img-selected'; }
						else { $checked = ''; }
					}

				$output .= '<span>';
				$output .= '<input type="radio" id="woo-radio-img-' . $value['id'] . $i . '" class="checkbox woo-radio-img-radio" value="'. esc_attr( $key ) .'" name="'. $value['id'].'" '.$checked.' />';
				$output .= '<div class="woo-radio-img-label">'. esc_html($key) .'</div>';
				$output .= '<img src="'.$option.'" alt="" class="woo-radio-img-img '. $selected .'" onClick="document.getElementById(\'woo-radio-img-'. $value['id'] . $i.'\').checked = true;" />';
				$output .= '</span>';

			}

		break;

		case "info":
			$default = $value['std'];
			$output .= $default;
		break;

		case "string_builder":
			$desc = $value['std'];
			$output .= '<div id="'.$value['id'].'">';
			$output .= 'Name<input class="woo-input woo-ignore" name="name" id="'. $value['id'] .'_name" type="text" />';
			$output .= 'Font Stack<input class="woo-input woo-ignore" name="value" id="'. $value['id'] .'_value" type="text" />';
			$output .= '<div class="add_button"><a class="button string_builder_add" href="#" class="string_builder" id="'.$value['id'].'">Add</a></div>';

			$output .= '<div id="'.$value['id'].'_return" class="string_builder_return">';
			$output .= '<h3>'.$desc.'</h3>';
			$saved_data = get_option($value['id']);
			if(!empty($saved_data)){
				foreach($saved_data as $name => $data){
					$data = stripslashes($data);
					$output .= '<div class="string_option" id="string_builer_option_'.str_replace( ' ','_',$name).'"><a class="delete" rel="'.$name.'" href="#"><img src="' .  ns_admin_path . '/images/ico-close.png" /></a><span>'.str_replace( '_',' ',$name) .':</span> '. $data .'</div>';
				}
			}
			$output .= '<div style="display:none" class="string_builder_empty">Nothing added yet.</div>';
			$output .= '</div>';
			$output .= '</div>';

		break;

		case "heading":

			if($counter >= 2){
			   $output .= '</div>'."\n";
			}
			$jquery_click_hook = preg_replace( '/[^a-zA-Z0-9\s]/', '', strtolower( $value['name'] ) );			
			$jquery_click_hook = str_replace(' ', '', $jquery_click_hook);

			$jquery_click_hook = "woo-option-" . $jquery_click_hook;
//			$jquery_click_hook = "woo-option-" . str_replace( "&","",str_replace( "/","",str_replace( ".","",str_replace( ")","",str_replace( "( ","",str_replace( " ","",strtolower($value['name'])))))));
			$menu .= '<li class="'.$value['icon'].'"><a title="'.  $value['name'] .'" href="#'.  $jquery_click_hook  .'">'.  $value['name'] .'</a></li>';
			$output .= '<div class="group" id="'. $jquery_click_hook  .'"><h2>'.$value['name'].'</h2>'."\n";
		break;
		}

		// if TYPE is an array, formatted into smaller inputs... ie smaller values
		if ( is_array($value['type'])) {
			foreach($value['type'] as $array){

					$id = $array['id'];
					$std = $array['std'];
					$saved_std = get_option($id);
					if($saved_std != $std){$std = $saved_std;}
					$meta = $array['meta'];

					if($array['type'] == 'text') { // Only text at this point

						 $output .= '<input class="input-text-small woo-input" name="'. $id .'" id="'. $id .'" type="text" value="'. esc_attr( $std ) .'" />';
						 $output .= '<span class="meta-two">'.$meta.'</span>';
					}
				}
		}
		if ( $value['type'] != "heading" ) {
			if ( $value['type'] != "checkbox" )
				{
				$output .= '<br/>';
				}
			$explain_value = ( isset( $value['desc'] ) ) ? $value['desc'] : '';
			if ( !current_user_can( 'unfiltered_html' ) && isset( $value['id'] ) && in_array( $value['id'], ns_disabled_if_not_unfiltered_html_option_keys() ) )
				$explain_value .= '<br /><br /><b> You are not able to update this option because you lack the <code>unfiltered_html</code> capability.</b>';
			$output .= '</div><div class="explain">'. $explain_value .'</div>'."\n";
			$output .= '<div class="clear"> </div></div></div>'."\n";
			}

	}

    //Checks if is not the Content Builder page
    if ( isset($_REQUEST['page']) && $_REQUEST['page'] != 'ns_content_builder' ) {
		$output .= '</div>';
	}

    return array($output,$menu);

}
endif; // ns_options_parser



if (!function_exists( 'ns_reset_options')):
/**
 * Reset site options
 *
 * @since Twenty Eleven 1.0
 */
function ns_reset_options( $options, $page = '' ){

	$excludes = array( 'blogname' , 'blogdescription' );

	foreach($options as $option){

		if(isset($option['id'])){
			$option_id = $option['id'];
			$option_type = $option['type'];

			//Skip assigned id's
			if(in_array($option_id,$excludes)) { continue; }

			if($option_type == 'multicheck'){
				foreach($option['options'] as $option_key => $option_option){
					$del = $option_id . "_" . $option_key;
					delete_option($del);
				}
			} else if(is_array($option_type)) {
				foreach($option_type as $inner_option){
					$option_id = $inner_option['id'];
					$del = $option_id;
					delete_option($option_id);
				}
			} else {
				delete_option($option_id);
			}
		}
	}
	
	//When Theme Options page is reset - Add the ns_options option
	if($page == 'ns'){
		delete_option( 'ns_options' );
	}
	
	// Reset theme
	ns_theme_uninstall();
}

endif; // ns_reset_options